package atm.com.controller.bankcard;

import atm.com.pojo.*;
import atm.com.service.bankcard.BankCardServiceInf;

import atm.com.util.AjaxResult;
import atm.com.util.CommonUtil;
import atm.com.util.MethodUtil;
import atm.com.util.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;

/**
 * AdminController class
 * @author di zhang
 * @date 2021/8/24
 */
@CrossOrigin(origins = "http://localhost:5325")
@RestController
public class BankCardController {

    @Autowired
    private BankCardServiceInf bankCardServiceInf;


    /**检查银行卡号和状态是否正确*/
    @RequestMapping("BankCardCheck")
    public String BankCardCheck(Bankcard bankcard){
    return  bankCardServiceInf.BankCardCheck(bankcard.getBankcardNumber());
    }

    /**检查银行卡密码是否正确*/
    @RequestMapping("BankCardLogin")
    public AjaxResult BankCardLogin(Bankcard bankcard, HttpSession session, Cashdispenser cashdispenser){
       return bankCardServiceInf.BankCardLogin(bankcard,session,cashdispenser);
    }

    /**三次输入错误 更改银行卡状态 冻结*/
    @RequestMapping("FrozenBankCard")
    public void FrozenBankCard(Bankcard bankcard){
        bankCardServiceInf.UpdateBankCardStatue(bankcard);
    }

    /**登出 写入日志表 删除session操作*/
    @RequestMapping("Logout")
    public String Logout(HttpSession httpSession){
        CommonUtil commonUtil=new CommonUtil();
        MethodUtil methodUtil = new MethodUtil();
        methodUtil.InsertJournal(httpSession,CommonUtil.operate.LOGIN_OUT);
        httpSession.removeAttribute(commonUtil.USER);
        httpSession.removeAttribute(commonUtil.BANK_CARD);
        httpSession.removeAttribute(commonUtil.CASH_DISPENSER);
        return commonUtil.STATE_LOGOUT;
    }

    /**查询 银行卡 余额*/
    @RequestMapping("GetBankCardBalance")
    public BigDecimal GetBankCardBalance(HttpSession session,CommonUtil commonUtil){
       Bankcard bankcard= (Bankcard) session.getAttribute(commonUtil.BANK_CARD);
        return  bankCardServiceInf.GetBanCardBalance(bankcard);
    }

    /**向用户银行卡 提款机 存款*/
    @RequestMapping("DepositBankCardBalance")
    public String  DepositBankCardBalance(BigDecimal money,HttpSession session){
        CommonUtil commonUtil =new CommonUtil();
        Bankcard bankcard=(Bankcard) session.getAttribute(commonUtil.BANK_CARD);
        Cashdispenser cashdispenser=(Cashdispenser) session.getAttribute(commonUtil.CASH_DISPENSER);
        System.out.println(money+"++++"+bankcard+"+++"+cashdispenser);
        //存款
        String status= bankCardServiceInf.DepositBanCardBalance(money,bankcard,cashdispenser);
        //存钱操作写入日志
        MethodUtil methodUtil =new MethodUtil();
        methodUtil.InsertJournal(session,CommonUtil.operate.DEPOSIT,money);
       return status;
    }

    /**向用户银行卡 提款机 取款*/
    @RequestMapping("TakeBankCardBalance")
    public String  TakeBankCardBalance(BigDecimal money,HttpSession session){
        CommonUtil commonUtil =new CommonUtil();
        Bankcard bankcard=(Bankcard) session.getAttribute(commonUtil.BANK_CARD);
        Cashdispenser cashdispenser=(Cashdispenser) session.getAttribute(commonUtil.CASH_DISPENSER);
        //取款
        String status= bankCardServiceInf.TakeBanCardBalance(money,bankcard,cashdispenser);
        //取款操作写入日志
        MethodUtil methodUtil=new MethodUtil();
        methodUtil.InsertJournal(session,CommonUtil.operate.WITHDRAW_MONEY,money);
        return status;
    }
    /**根据银行卡号 查询用户信息*/
    @RequestMapping("GetUserByBankCard")
    public User GetUserByBankCard(HttpSession session){
        CommonUtil commonUtil=new CommonUtil();
        return (User) session.getAttribute(commonUtil.USER);
    }



    }
